package com.tronsis.library.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tronsis.library.R;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.widget.LoadingProgressDialog;


/**
 * Fragment的基类
 * @author allen@tronsis.com
 */
public abstract class BaseFragment extends Fragment {
protected Context mContext;
    private LoadingProgressDialog mLoadingProgressDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = onCreateView();
        initData();
        LogUtil.d("Page", "onCreateView");
        return rootView;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.d("Page", "onCreate");
        mContext = getContext();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            LogUtil.d("Page", "Visible....." +  this.getClass().getName());
            onVisible();
        } else {
            onInvisible();
        }
    }

    /**
     * Fragment可见
     */
    protected void onVisible() {
        LogUtil.d("Page", "Visible");
    }

    /**
     * Fragment不可见
     */
    public void onInvisible() {

    }

    /**
     * 相当于onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
     * savedInstanceState)
     * @return
     */
    protected abstract View onCreateView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 在Fragment中处理返回事件
     */
    public void onBackPressed() {
    }


    protected void showLoadingDialog() {
        showLoadingDialog("");
    }


    protected void showLoadingDialog(String str) {
        if (mLoadingProgressDialog == null) {
            mLoadingProgressDialog = new LoadingProgressDialog(getContext(), R.style.CustomProgressDialog);
            mLoadingProgressDialog.show(str);
        } else {
            mLoadingProgressDialog.show(str);
        }

    }

    protected void dismissLoadingDialog() {
        if (mLoadingProgressDialog != null) {
            mLoadingProgressDialog.dismiss();
        }
    }


}
